import ImChat from './modules/ImChat'
import ImTrans from './modules/ImTrans'
import ImUser from './modules/ImUser'
import ImSystem from './modules/ImSystem'
import ImAfterSales from './modules/ImAfterSales'
import ImSettlementBill from './modules/ImSettlementBill'
import SkuCount from './modules/skuCount'
import ImageCache from './modules/imgCache'

export function sqlInit(isInit: boolean) {
    let sqlite = {
        imChat: new ImChat(),
        imTrans: new ImTrans(),
        imUser: new ImUser(),
        imSystem: new ImSystem(),
        imAfterSales: new ImAfterSales(),
        imSettlementBill: new ImSettlementBill(),
        skuCount: new SkuCount(),
        img: new ImageCache()
    }
    if (isInit) {
        // 初始化表结构
        let optionsArr = []
        // 聊天
        optionsArr.push(sqlite.imChat.executeOptions({ model: sqlite.imChat.CREATE }))
        optionsArr.push(...sqlite.imChat.executeOptionsByTableIndex(sqlite.imChat.tableIndexArr))
        // 交易物流
        optionsArr.push(sqlite.imTrans.executeOptions({ model: sqlite.imTrans.CREATE }))
        optionsArr.push(...sqlite.imTrans.executeOptionsByTableIndex(sqlite.imTrans.tableIndexArr))
        // 用户
        optionsArr.push(sqlite.imUser.executeOptions({ model: sqlite.imUser.CREATE }))
        optionsArr.push(...sqlite.imUser.executeOptionsByColumnAdd(sqlite.imUser.columnAdd))
        // 系统通知
        optionsArr.push(sqlite.imSystem.executeOptions({ model: sqlite.imSystem.CREATE }))
        optionsArr.push(...sqlite.imSystem.executeOptionsByTableIndex(sqlite.imSystem.tableIndexArr))
        // 售后通知
        optionsArr.push(sqlite.imAfterSales.executeOptions({ model: sqlite.imAfterSales.CREATE }))
        optionsArr.push(...sqlite.imAfterSales.executeOptionsByTableIndex(sqlite.imAfterSales.tableIndexArr))
        // 账单通知
        optionsArr.push(sqlite.imSettlementBill.executeOptions({ model: sqlite.imSettlementBill.CREATE }))
        optionsArr.push(...sqlite.imSettlementBill.executeOptionsByTableIndex(sqlite.imSettlementBill.tableIndexArr))

        optionsArr.push(sqlite.skuCount.executeOptions({ model: sqlite.skuCount.CREATE }))
        // 图片缓存
        optionsArr.push(sqlite.img.executeOptions({ model: sqlite.img.CREATE }))
        optionsArr.push(...sqlite.img.executeOptionsByTableIndex(sqlite.img.tableIndexArr))


        sqlite.imChat
            .executeTransaction(optionsArr)
            .then(() => {})
            .catch(err => {
                console.log('初始化表失败', err)
            })
    }

    return sqlite
}
